package com.xmkj.bearuser.my;

import android.view.View;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.common.Entity.AddressEntity;
import com.common.mvp.BaseMvpActivity;
import com.common.mvp.BasePresenter;
import com.common.retrofit.method.AddAddressMethods;
import com.common.retrofit.method.ModifyAddressMethods;
import com.common.retrofit.subscriber.CommonSubscriber;
import com.common.retrofit.subscriber.SubscriberListener;
import com.common.rxbus.RxBus;
import com.common.rxbus.postevent.RxKeyEvent;
import com.common.utils.EmptyUtils;
import com.xmkj.bearuser.R;
import com.xmkj.bearuser.utils.BottomPickerUtils;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Create by leo
 * 创建日期：2018/5/10
 * 描述：
 */

public class AddressModifyActivity extends BaseMvpActivity {
    public static final String FROM = "from";
    @BindView(R.id.tv_person)
    TextView tvPerson;
    @BindView(R.id.et_name)
    EditText etName;
    @BindView(R.id.tv_phone)
    TextView tvPhone;
    @BindView(R.id.et_phone)
    EditText etPhone;
    @BindView(R.id.tv_place)
    TextView tvPlace;
    @BindView(R.id.tv_address)
    TextView tvAddress;
    @BindView(R.id.tv_address_detail)
    TextView tvAddressDetail;
    @BindView(R.id.et_address_details)
    EditText etAddressDetails;
    @BindView(R.id.tv_add)
    TextView tvAdd;
    @BindView(R.id.rl_choose_address)
    RelativeLayout rl_choose_address;

    private boolean isAdd = false;
    private AddressEntity.ListsBean bean;
    private String address;

    @Override
    protected BasePresenter createPresenterInstance() {
        return null;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_address_modify;
    }

    @Override
    protected void onViewCreated() {
        bean = (AddressEntity.ListsBean) getIntent().getSerializableExtra(FROM);
        if (bean != null) {
            etName.setText(bean.username);
            etPhone.setText(bean.mobile);
            etAddressDetails.setText(bean.address);

            address = bean.area;
            tvAddress.setText(bean.area);
            isAdd = false;
            setNavigationBack("修改收货地址");
            tvAdd.setText("修改");

        } else {
            isAdd = true;
            setNavigationBack("添加收货地址");
            tvAdd.setText("添加");

        }
    }

    @Override
    protected void doLogicFunc() {
            attachClickListener(tvAdd);
            attachClickListener(rl_choose_address);
    }

    @Override
    protected void onViewClicked(View view) {
        if(view.getId()==tvAdd.getId()){
            if(isAdd){ // 添加
                if(EmptyUtils.isEmpty(address) || EmptyUtils.isEmpty(getEditTextStr(etName))||EmptyUtils.isEmpty(getEditTextStr(etPhone))||EmptyUtils.isEmpty(getEditTextStr(etAddressDetails))){
                    showToastMsg("请输入完整信息");
                    return;
                }
                gotoAddAdress();
            }else { // 修改
                if(EmptyUtils.isEmpty(address) || EmptyUtils.isEmpty(getEditTextStr(etName))||EmptyUtils.isEmpty(getEditTextStr(etPhone))||EmptyUtils.isEmpty(getEditTextStr(etAddressDetails))){
                    showToastMsg("请输入完整信息");
                    return;
                }
                gotModify();
            }
        }else if(rl_choose_address.getId()==view.getId()){
            BottomPickerUtils.showCityPicker(this, new BottomPickerUtils.CityOptionPickerCallback() {
                @Override
                public void onOptionSelect(String option1, String option2, String option3) {
//                    tvSheng.setText(option1);
//                    tvShi.setText(option2);
//                    tvXian.setText(option3);
//                    has_choose_address = true;
//                    province_str = option1;
//                    city_str = option2;
//                    area_str = option3;
                    address = option1+"/"+ option2+"/"+option3;
                    tvAddress.setText(address);

                }
            });
        }
    }

    private void gotModify() {
        showProgressingDialog();
        CommonSubscriber<Object> subscriber = new CommonSubscriber<>(new SubscriberListener() {
            @Override
            public void onNext(Object o) {
                dismissProgressDialog();
                showToastMsg("修改成功");
                RxBus.getDefault().postSticky(new RxKeyEvent(RxKeyEvent.ADD_ADDRESS, true));
                onBackPressed();

//                gotoHttpRep();
            }

            @Override
            public void onError(String e, int code) {
                dismissProgressDialog();
                showToastMsg(e);
            }
        });
        ModifyAddressMethods.getInstance().updateAddress(subscriber, uid, hashid, getEditTextStr(etName),getEditTextStr(etPhone),address,getEditTextStr(etAddressDetails),bean.addressid);
        rxManager.add(subscriber);
    }

    private void gotoAddAdress() {
        showProgressingDialog();
        CommonSubscriber<Object> subscriber = new CommonSubscriber<>(new SubscriberListener() {
            @Override
            public void onNext(Object o) {
                dismissProgressDialog();
                showToastMsg("添加成功");
                RxBus.getDefault().postSticky(new RxKeyEvent(RxKeyEvent.ADD_ADDRESS, true));
                onBackPressed();

//                gotoHttpRep();
            }

            @Override
            public void onError(String e, int code) {
                dismissProgressDialog();
                showToastMsg(e);
            }
        });
        AddAddressMethods.getInstance().addAddress(subscriber, uid, hashid, getEditTextStr(etName),getEditTextStr(etPhone),address,getEditTextStr(etAddressDetails));
        rxManager.add(subscriber);
    }

    @Override
    protected void setNavigation() {


    }


    @Override
    public void bindKnife() {
        ButterKnife.bind(this);
    }
}
